package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;

import java.nio.file.NotLinkException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     *
     * @param setmealDto
     */
    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {

        setmealService.save(setmealDto);

        Long setmealId = setmealDto.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);

        return R.success("新套餐添加成功!");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        Page<Setmeal> PageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.like(name != null, Setmeal::getName, name);

        setmealService.page(PageInfo, setmeallqw);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        BeanUtils.copyProperties(PageInfo, setmealDtoPage, "records");

        List<Setmeal> records = PageInfo.getRecords();
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            Category Id = categoryService.getById(setmealDto.getCategoryId());
            if (Id != null) {
                String name1 = Id.getName();
                setmealDto.setCategoryName(name1);
            }
            setmealDtos.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtos);

        return R.success(setmealDtoPage);
    }

    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deletebyid(@RequestParam List<Long> ids) {
        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.in(Setmeal::getId, ids);
        setmeallqw.eq(Setmeal::getStatus, 1);
        int count = setmealService.count(setmeallqw);
        if (count > 0) {
            throw new CustomException("该套餐正在售卖中,无法删除!");
        }

        setmealService.removeByIds(ids);

        LambdaQueryWrapper<SetmealDish> SetmealDishlqw = new LambdaQueryWrapper<>();
        SetmealDishlqw.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(SetmealDishlqw);
        return R.success("删除套餐成功!");
    }

    /**
     * 修改状态
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R updatebyid(@PathVariable Integer status, @RequestParam List<Long> ids) {

        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.in(Setmeal::getId, ids);
        List<Setmeal> list = setmealService.list(setmeallqw);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setStatus(status);
        }
        setmealService.updateBatchById(list);

        return R.success("修改成功!");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {

        LambdaQueryWrapper<Setmeal> setmeallqw = new LambdaQueryWrapper<>();
        setmeallqw.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        setmeallqw.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        setmeallqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(setmeallqw);

        return R.success(setmealList);
    }
    @GetMapping("/{id}")
    private R<SetmealDto> get(@PathVariable Long id){

        SetmealDto setmealDto = setmealService.getByIdWithSetmeal(id);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithSetmeal(setmealDto);

        return R.success("修改成功!");
    }
}
